package com.sy.simpleegg.platform.kkb.network.interceptor

import android.util.Log
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/**
 * @author sy
 * @date 2024/10/11 15:17
 * @desc
 */
class RetryInterceptor(maxRetry: Int): Interceptor {
    var maxRetry = 0
    private var retryNum = 0

    init {
        this.maxRetry = maxRetry
    }

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        Log.i("okHttp", "retryNum = " + retryNum)
        var response: Response
        response = chain.proceed(request)
        while (!response.isSuccessful && retryNum < maxRetry) {
            ++retryNum
            Log.i("okHttp", "retryNum = " + retryNum)
            response.close()
            response = chain.proceed(request)
        }
        return response
    }
}